package org.ourguy.mymoviesandshows;

import java.util.ArrayList;

import org.ourguy.mymoviesandshows.adapters.ShowsAdapter;
import org.ourguy.mymoviesandshows.datamodel.ITitle;
import org.ourguy.mymoviesandshows.managers.DataAccessManager;

import android.app.ListActivity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.GestureDetector;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class TrackingListActivity extends ListActivity {
	
	private ShowsAdapter adapter;
	private DataAccessManager menager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		registerForContextMenu(getListView());
		menager = DataAccessManager.getInstance(this);
	     
		getListView().setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					startNewActivity(position);
				}
		});
		
		// sliding between tabs
		final TabActivity parrent = (TabActivity) getParent();
		final GestureDetector mGestureDetector = new GestureDetector(this,
				new GestureDetector.SimpleOnGestureListener() {

					public boolean onFling(MotionEvent e1, MotionEvent e2,
							float velocityX, float velocityY) {
						int dx = (int) (e2.getX() - e1.getX());
						// don't accept the fling if it's too short
						// as it may conflict with a button push
						if (Math.abs(dx) > 300 && Math.abs(velocityX) > 200) {
							if (velocityX > 0) {
							} else {
								parrent.getTabHost().setCurrentTab(1);
							}
							return true;
						} else {
							return false;
						}
					}
				});

		View.OnTouchListener gestureListener = new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				return mGestureDetector.onTouchEvent(event);
			}
		};

		getListView().setOnTouchListener(gestureListener);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		ArrayList<ITitle> titles = menager.getFavorites();
		adapter = new ShowsAdapter(this, titles);
	    setListAdapter(adapter);
		
	}
	
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        
        AdapterView.AdapterContextMenuInfo info =
                (AdapterView.AdapterContextMenuInfo) menuInfo;
        
        menu.setHeaderTitle(((ITitle)adapter.getItem(info.position)).getTitle());
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu_tracking_list, menu);
    }
	
	private void startNewActivity(int position)
    {
    	Intent intent = new Intent(getApplicationContext(), ShowActivity.class);
    	ITitle show = (ITitle)getListAdapter().getItem(position);
    	intent.putExtra("show", show.getApiId());
		this.startActivity(intent);
    }
}